package com.hy.proxy.common;

import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: hy
 * @Date: Created in 2019/9/23/15:13
 * @Description: 网络请求工具类
 * @Version: 1.0
 */
public class HttpUtils {

    /**
     * 将String的Headers信息转换为Map
     *
     * @param headersStr 头部信息
     * @return HashMap的Headers信息
     */
    public static Map<String, String> createHeadersMap(String headersStr) {

        String pattern = "([-a-zA-Z]+): ([^0-9].*)";

        Matcher m = Pattern.compile(pattern).matcher(headersStr.trim());

        Map<String, String> headersMap = new Hashtable<>();

        while (m.find()) {

            //分割字符 格式为>>Host: free-ss.site
            String[] split = m.group().split(": ");

            //将分割的字符存入Map
            headersMap.put(split[0], split[1]);
        }

        return headersMap;
    }
}
